


//% color="#AA278D" iconWidth=50 iconHeight=40
namespace kljmi2cconnect {
    let address=`0x0`;
    // let msglength=0;
    // let globalmsg="";
       //% block="启用I2C通信功能（必选，放第一个）" blockType="command"
       export function include(parameter: any, block: any){
        Generator.addInclude('Wire', '#include <Wire.h>');
       
       }
      //% block="---"
        export function noteSep() {

        }

    
    
  
       //% block="设定当前设备为父设备" blockType="command"
       export function masterbegin(parameter: any, block: any){
        Generator.addSetupMainTop('Wire.begin',`Wire.begin();`);
    }


     //% block="设定当前设备为子设备 编号为:[ADDRESS]" blockType="command"
    //% ADDRESS.shadow="range" ADDRESS.params.min=0 ADDRESS.params.max=127 ADDRESS.defl=0
    export function setslaveaddress(parameter: any, block: any){
        let num = parameter.ADDRESS.code;
        let hexStr = num.toString(16);
        address='0x'+hexStr;
        Generator.addDefine(`ADDRESS`,address);
        Generator.addSetupMainTop('Wire.begin',`Wire.begin(${address});`);  
    }



 ////////////////////////////////////////////////////////
        //% block="---"
        export function noteSep2() {

        }

       
    
      //% block="当子设备收到父设备数据时" blockType="hat"          
      export function readyreceiveEvent(parameter: any, block: any){
        let eventname='onreceiveEvent';
        Generator.addSetup(eventname,`Wire.onReceive(${eventname});`,false);  
         Generator.addEvent(eventname, "void", eventname, "", false); 
        
      }
     //% block="当子设备向父设备返回数据时" blockType="hat"          
     export function readyrequestEvent(parameter: any, block: any){
        let eventname2='onrequestEvent';
        Generator.addSetup(eventname2,`Wire.onRequest(${eventname2});`,true);  
        Generator.addEvent('onrequestEvent', "void", 'onrequestEvent', "", false); 
     
      }
       
   //% block="---"
   export function noteSep3() {

   }
    
    
   
     //% block="打开传送数据到编号为:[ADDRESS]的子设备开关" blockType="command"
    //% ADDRESS.shadow="range" ADDRESS.params.min=0 ADDRESS.params.max=127 ADDRESS.defl=0
    export function beginTransmission(parameter: any, block: any){
        let num = parameter.ADDRESS.code;
        let hexStr = num.toString(16);
        address='0x'+hexStr;
        Generator.addCode(`Wire.beginTransmission(${address});`);
    }
    //% block="发送数据:[data]" blockType="command"
    //% data.shadow="string" 
    export function write(parameter: any, block: any){
        let data=parameter.data.code;
        Generator.addCode(`Wire.write(${data});`);
    }

   
    //% block="关闭传送数据开关" blockType="command"
    export function endTransmission(parameter: any, block: any){
        Generator.addCode(`Wire.endTransmission();`);
    }

     //% block="父设备向编号为:[ADDRESS]的子设备索要[COUNT]字节的数据" blockType="command"
    //% ADDRESS.shadow="range" ADDRESS.params.min=0 ADDRESS.params.max=127 ADDRESS.defl=0
    //% COUNT.shadow="number" COUNT.defl=1
    export function requestFrom(parameter: any, block: any){
        let num = parameter.ADDRESS.code;
        let hexStr = num.toString(16);
        address='0x'+hexStr;
        let count=parameter.COUNT.code;
        Generator.addCode(`Wire.requestFrom(${address},${count});`);
    }

    //% block="---"
    export function noteSep4() {

    }
     //% block="接收到的信息长度（单位：字节）" blockType="reporter"
     export function onreciveEvent(parameter: any, block: any){
        Generator.addCode(['Wire.available()',Generator.ORDER_UNARY_POSTFIX]);
     
     }
    //% block="接收1字节数据" blockType="reporter"
    export function read(parameter: any, block: any){
        Generator.addCode(['Wire.read()',Generator.ORDER_UNARY_POSTFIX]);
    }
    
    
  
    function replace(str :string) {
        return str.replace(/"/g, '');
    }
     
}
